package com.synergy.stockflow.service;

import com.synergy.stockflow.repositories.UserDetailsMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class UserService implements UserDetailsService {

    private final UserDetailsMapper userDetailsMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        if (username == null || username.trim().isEmpty()) {
            throw new UsernameNotFoundException("用户名不能为空");
        }
        UserDetails userDetails = userDetailsMapper.findUserByUsername(username);
        if (userDetails == null){
            throw new UsernameNotFoundException("用户不存在/存在账号异常");
        }
        return userDetails;
    }
}
